package com.pc.admin.controller;

import com.pc.admin.dto.DaDaExcelDTO;
import com.pc.admin.entity.DaDa;
import com.pc.admin.service.DaDaService;
import com.pc.admin.util.CommonResult;
import com.pc.admin.util.ExcelUtils;
import com.pc.admin.vo.DaDaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author : ZJ
 * @create 2023/3/11 20:24
 */


@Api(tags = "大大管理")
@RestController
@RequestMapping(value = "/DaDa")
public class DaDaController {

    @Autowired
    private DaDaService daDaService;

    /**
     * 增加 编辑
     * @return
     */
    @ApiOperation(value = "增加，编辑",notes = "增加，编辑")
    @PostMapping("/addDaDa")
    public CommonResult addDaDa(@RequestBody DaDaVO daDaVO) {
        return CommonResult.ok(daDaService.addDaDa(daDaVO));
    }


    /**
     * 查询列表
     * @return
     */
    @ApiOperation(value = "查询列表",notes = "查询列表")
    @PostMapping("/queryDaDaList")
    public CommonResult queryDaDaList() {
        List<DaDa> daDaList = daDaService.queryDaDaList();
        return CommonResult.ok(daDaList);
    }


    /**
     * 查询详情
     * @return
     */
    @ApiOperation(value = "查询详情",notes = "查询详情")
    @GetMapping("/detailsDaDa")
    public CommonResult<DaDaVO> detailsDaDa(Integer id) {
        daDaService.detailsDaDa(id);
        return CommonResult.ok();
    }



    /**
     * 删除
     * @return
     */
    @ApiOperation(value = "删除",notes = "删除")
    @GetMapping("/delDaDa")
    public CommonResult delDaDa(Integer id) {
        return daDaService.delDaDa(id);
    }




    @ApiOperation(value = "导出excel", notes = "导出excel")
    @PostMapping("/exportExcel")
    //导出时不能使用CommonResult返回值，要用void才不会报错（报错：Cannot call sendError() after the response has been committed）
    public void exportExcel(@RequestBody DaDaExcelDTO excelDTO, HttpServletResponse response) throws IOException {
        daDaService.exportExcel(excelDTO, response);

    }


    //  https://blog.csdn.net/sunnyzyq/article/details/121994504  导入导出地址分享
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        // 表头数据
        List<Object> head = Arrays.asList("姓名","年龄","性别","头像");
        // 用户1数据
        List<Object> user1 = new ArrayList<>();
        user1.add("诸葛亮");
        user1.add(60);
        user1.add("男");
        user1.add("https://profile.csdnimg.cn/A/7/3/3_sunnyzyq");
        // 用户2数据
        List<Object> user2 = new ArrayList<>();
        user2.add("大乔");
        user2.add(28);
        user2.add("女");
        user2.add("https://profile.csdnimg.cn/6/1/9/0_m0_48717371");
        // 将数据汇总
        List<List<Object>> sheetDataList = new ArrayList<>();
        sheetDataList.add(head);
        sheetDataList.add(user1);
        sheetDataList.add(user2);
        // 导出数据
        ExcelUtils.export(response,"用户表", sheetDataList);
    }



}
